<?php
/**
 * Created by PhpStorm.
 * User: zhongbaitong
 * Date: 2020/1/10
 * Time: 14:37
 */

namespace app\im\home;

use app\common\controller\ApiBase;
use app\im\model\ChatDeletion;
use app\im\model\ChatLists;
use app\im\model\Chat as ChatModel;
use app\im\model\User;

/**
 * 即时通讯
 * Class Chat
 * @package app\im\home
 */
class Chat extends ApiBase
{

    /**
     * 聊天列表
     * @return \think\response\Json
     * @throws \app\common\exception\ApiError
     */
    public function lists()
    {
        $input = $this->getInput("", 1);

        $data = ChatLists::getLists($input);

        return $this->apiReturn(compact("data", "input"));
    }


    /**
     * 聊天记录
     * @return \think\response\Json
     * @throws \app\common\exception\ApiError
     */
    public function history()
    {
        $input = $this->getInput("obj_uid", 1);

        // 聊天记录
        $data = ChatModel::getHistory($input);
        // 消息设置已读
        if ($data["root_id"])
            ChatLists::setReaded($input["user_id"], $data["root_id"]);

        return $this->apiReturn($data);
    }

    /**
     * 保存消息
     * chat_type 1-文本 2-图片 3-未读音频(4-已读) 5-未领红包 6-已领红包 7-过期红包
     * root_type 1-私聊 2-群聊
     * @return \think\response\Json
     * @throws \app\common\exception\ApiError
     * @throws \think\Exception
     */
    public function save()
    {
        $input = $this->getInput("obj_uid,root_id,content", 1);

        $chat = ChatModel::newMess($input);

        return $this->apiReturn(compact("chat"));
    }

    /**
     * 列表删除消息
     * @return \think\response\Json
     * @throws \app\common\exception\ApiError
     */
    public function listDelete()
    {
        $input = $this->getInput("list_id", 1);
        $user_id = $input["user_id"];

        // 列表删除
        $list = ChatLists::get($input["list_id"]);
        if ($list->is_del || $list->user_id != $user_id)
            return $this->apiError("无法删除~");
        $list->is_del = 1;
        $list->save();

        // 记录删除
        $chat_ids = ChatModel::where("user_id", $user_id)
            ->where("root_id", $list->root_id)
            ->column("id");
        $data = [];
        foreach ($chat_ids as $key => $chat_id) {
            $data[] = compact("user_id", "chat_id");
        }
        (new ChatDeletion)->saveAll($data);

        return $this->apiSuccess();
    }


    /**
     * 记录删除
     * @return \think\response\Json
     * @throws \app\common\exception\ApiError
     */
    public function chatDelete()
    {
        $input = $this->getInput("chat_id");

        $delete = new ChatDeletion;
        $delete->chat_id = $input["chat_id"];
        $delete->user_id = $this->user["id"];
        $delete->save();

        return $this->apiSuccess();
    }

    /**
     * 全部未读数
     * @return \think\response\Json
     * @throws \think\Exception\DbException
     */
    public function getAllUnreadNum()
    {
        $user_id = $this->user["id"];

        $result = ChatLists::getUnReadInfo($user_id);

        // 未读用户s
        $users = User::where("id", "in", $result["obj_uids"])
            ->all();

        // 总未读数
        $sum = $result['chat_count'];

        $data = compact("sum", "users");

        return $this->apiReturn(compact("data"));
    }

    /**
     * 全部设置已读
     * @return \think\response\Json
     */
    public function setAllReaded()
    {
        $user_id = $this->user["id"];
        $root_ids = ChatLists::getRootIDs($user_id);

        ChatLists::where("root_id", "in", $root_ids)
            ->where("user_id", "neq", $user_id)
            ->setField("unread_num", 0);

        return $this->apiSuccess();
    }

    /**
     * 设置音频已读
     * @throws \app\common\exception\ApiError
     */
    public function setAudioReaded()
    {
        $input = $this->getInput('chat_id');

        if (3 == ChatModel::getValueByID($input['chat_id'], 'chat_type')) {
            ChatModel::setValueById($input['chat_id'], 'chat_type', 4);
            return $this->apiSuccess();
        }

        return $this->apiError();
    }

}